home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / utils / init / magic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  3.6 KB  |  160 lines

  1. /*
  2.  *    magic.c        - magic number management routines
  3.  *
  4.  *    XXX eventually, should be able to handle version identifiers
  5.  *    of length != 4.
  6.  *
  7.  *  STANDALONE CODE - do not use error routines as this code is linked with
  8.  *  stuff that does not cinterface.a
  9.  */
  10.  
  11. #include <sys/file.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <ctype.h>
  15. #include <strings.h>
  16.  
  17. #include "tmp/postgres.h"
  18.  
  19. RcsId("$Header: /private/postgres/src/utils/init/RCS/magic.c,v 1.11 1991/11/12 20:20:29 mer Exp $");
  20.  
  21. #include "utils/log.h"
  22. #include "tmp/miscadmin.h"
  23.  
  24. static char    Pg_verfile[] = PG_VERFILE;
  25.  
  26. /*
  27.  * private function prototypes
  28.  */
  29. static PathSetVersionFilePath ARGS ((char path[], char filepathbuf[]));
  30.  
  31. /*
  32.  *  Noversion moved to globals.c
  33.  */
  34.  
  35. /*
  36.  * DatabaseMetaGunkIsConsistent
  37.  *
  38.  * Returns 1 iff all version numbers and ownerships are consistent.
  39.  *
  40.  * Note that we have to go through the whole rigmarole of generating the path
  41.  * and checking the existence of the database whether Noversion is set or not.
  42.  */
  43.  
  44. int
  45. DatabaseMetaGunkIsConsistent(database, path)
  46.     char    database[], path[];
  47. {
  48.     int        isValid;
  49.     extern char    *GetDataHome();
  50.     char        *home = GetDataHome();
  51.     struct stat    statbuf;
  52.  
  53.     sprintf(path, "%s/data\0", home);
  54.     isValid = ValidPgVersion(path);
  55.     sprintf(path, "%s/data/base/%s\0", home, database);
  56.     isValid = ValidPgVersion(path) || isValid;
  57.     
  58.     if (stat(path, &statbuf) < 0)
  59.         elog(FATAL, "database %s does not exist, bailing out...",
  60.              database);
  61.     
  62.     return(isValid);
  63. }
  64.  
  65.  
  66. /*
  67.  *    ValidPgVersion    - verifies the consistency of the database
  68.  *
  69.  *    Returns 1 iff the catalog version number (from the version number file
  70.  *    in the directory specified in "path") is consistent with the backend
  71.  *    version number.
  72.  */
  73.  
  74. int
  75. ValidPgVersion(path)
  76.     char    path[];
  77. {
  78.     int        fd;
  79.     char        version[4], buf[MAXPGPATH+1];
  80.     struct stat    statbuf;
  81.     u_short        my_euid = geteuid();
  82.     
  83.     PathSetVersionFilePath(path, buf);
  84.     
  85.     if (stat(buf, &statbuf) >= 0) {
  86.         if (statbuf.st_uid != my_euid && my_euid != 0)
  87.             elog(FATAL,
  88.                  "process userid (%d) != database owner (%d)",
  89.                 my_euid, statbuf.st_uid);
  90.     } else
  91.         return(0);
  92.     
  93.     if ((fd = open(buf, O_RDONLY, 0)) < 0) {
  94.         if (!Noversion)
  95.             elog(DEBUG, "ValidPgVersion: %s: %m", buf);
  96.         return(0);
  97.     }
  98.  
  99.     if (read(fd, version, 4) < 4 ||
  100.         !isascii(version[0]) || !isdigit(version[0]) ||
  101.         version[1] != '.' ||
  102.         !isascii(version[2]) || !isdigit(version[2]) ||
  103.         version[3] != '\n')
  104.         elog(FATAL, "ValidPgVersion: %s: bad format", buf);
  105.     if (version[2] != '0' + PG_VERSION ||
  106.         version[0] != '0' + PG_RELEASE) {
  107.         if (!Noversion)
  108.             elog(DEBUG,
  109.                  "ValidPgVersion: should be %d.%d not %c.%c",
  110.                  PG_RELEASE, PG_VERSION, version[0], version[2]);
  111.         close(fd);
  112.         return(0);
  113.     }
  114.     close(fd);
  115.     return(1);
  116. }
  117.  
  118.  
  119. /*
  120.  *    SetPgVersion    - writes the version to a database directory
  121.  */
  122.  
  123. SetPgVersion(path)
  124.     char    path[];
  125. {
  126.     int    fd;
  127.     char    version[4], buf[MAXPGPATH+1];
  128.  
  129.     PathSetVersionFilePath(path, buf);
  130.     
  131.     if ((fd = open(buf, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0)
  132.         elog(FATAL, "SetPgVersion: %s: %m", buf);
  133.  
  134.     version[0] = '0' + PG_RELEASE;
  135.     version[1] = '.';
  136.     version[2] = '0' + PG_VERSION;
  137.     version[3] = '\n';
  138.     if (write(fd, version, 4) != 4)
  139.         elog(WARN, "SetPgVersion: %s: %m", buf);
  140.  
  141.     close(fd);
  142. }
  143.  
  144.  
  145. /*
  146.  * PathSetVersionFilePath
  147.  *
  148.  * Destructively change "filepathbuf" to contain the concatenation of "path"
  149.  * and the name of the version file name.
  150.  */
  151.  
  152. static
  153. PathSetVersionFilePath(path, filepathbuf)
  154.     char    path[], filepathbuf[];
  155. {
  156.     if (strlen(path) > (MAXPGPATH - sizeof(Pg_verfile) - 1))
  157.         elog(FATAL, "PathSetVersionFilePath: %s: path too long");
  158.     (void) sprintf(filepathbuf, "%s/%s\0", path, Pg_verfile);
  159. }
  160.